/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.data.libs.LibEvent;
import com.bourre.data.libs.LibStack;
import com.bourre.events.EventType;
import com.bourre.events.StringEvent;

import fever.conf.ConfDebug;
import fever.conf.parsers.ConfigurationParser;
import fever.exception.IllegalAccessException;

/**
 * Basic implementation for configuration parser that use {@code GraphicLib} loading 
 * feature. 
 *
 * @author Romain Ecarnot
 */
class fever.conf.parsers.LibParser extends ConfigurationParser
{
	//-------------------------------------------------------------------------
	// Events definition
	//-------------------------------------------------------------------------
	
	/** Broadcasted during lib loading. */
	public static var onParserProgressEVENT : EventType = new EventType( 'onParserProgress' );
	
		
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _stack : LibStack;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Starts parsing.
	 * 
	 * <p>Broadcasts {@link #onParserStartEVENT} event type.
	 */
	public function parse() : Void
	{
		super.parse();
		
		_initStack();
		
		if( !_stack.isEmpty() )
		{
			_stack.prefixURL( _getLibPrefix() );
			_stack.load();
		}
		else _onParseComplete();
	}	
	
	/**
	 * Returns human readable text indicating parsing state.
	 */
	public function getParsingLabel() : String
	{
		return 'loading libraries';
	}
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
				
	/**
	 * Constructor.
	 */
	private function LibParser()
	{
		super();
		
		_stack = new LibStack();
		_stack.setTimeOut( 2000 );
		_stack.setAntiCache( false );
		
		_initStackEvent();
	}
	
	private function _initStack() : Void
	{
		throw new IllegalAccessException( '_initStack() Must be override in sub parser class' );
	}
	
	private function _getLibPrefix() : String
	{	
		return "";
	}
	
	private function _initStackEvent() : Void
	{
		_stack.addEventListener( LibStack.onLoadInitEVENT, this, _onLoadInit );		_stack.addEventListener( LibStack.onTimeOutEVENT, this, _onTimeOut );		_stack.addEventListener( LibStack.onLoadProgressEVENT, this, _onProgress );
				_stack.addEventListener( LibStack.onLoadCompleteEVENT, this, _onParseComplete );
	}
	
	private function _releaseStackEvent() : Void
	{
		_stack.removeEventListener( LibStack.onLoadInitEVENT, this );
		_stack.removeEventListener( LibStack.onTimeOutEVENT, this );		_stack.removeEventListener( LibStack.onLoadProgressEVENT, this );
		
		_stack.removeEventListener( LibStack.onLoadCompleteEVENT, this );
	}
	
	private function _onLoadInit( event : LibEvent) : Void
	{
		ConfDebug.DEBUG( event.getLib().getURL() + ' is loaded' );
	}
	
	private function _onProgress( event : LibEvent ) : Void
	{
		_fireEvent( 
			new StringEvent( 
				onParserProgressEVENT, 
				" " + event.getPerCent() + " %"
			) 
		);
	}
	
	private function _onTimeOut( event : LibEvent) : Void
	{
		ConfDebug.ERROR( event.getLib().getURL() + ' timeout' );
	}
	
	private function _onParseComplete( event : LibEvent ) : Void
	{		
		_releaseStackEvent();
		_fireEventType( onParserCompleteEVENT );
	}
	
}